//
//  ForgivePassWordViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/10.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ForgivePassWordViewController.h"

@interface ForgivePassWordViewController ()

@property (nonatomic ,weak)UIImageView * backImageView;
@property (nonatomic, strong)UITextField * accountText;
@property (nonatomic, strong)UITextField * passwordText;
@property (nonatomic, strong)UITextField * repasswordText;
@property (nonatomic, weak)UIButton * sureButton;
@property (nonatomic, weak)UIButton * verifyButton;
@property (nonatomic, weak)UIButton * backButton;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UILabel * noAccLabel;

//计时器
@property (nonatomic, strong)NSTimer * countDownTimer;

@end

//倒计时总的秒数
static NSInteger  secondsCountDown = 60;

@implementation ForgivePassWordViewController


//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
//}

//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
//}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;

    [self setupUI];
}


- (void)setupUI{
    
    
    UIImageView * backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    backImageView.image = ImageNamed(@"bg");
    backImageView.userInteractionEnabled = YES;
    
    self.backImageView = backImageView;
    
    [self.view addSubview:self.backImageView];
    
    
    UIButton * backButton = [[UIButton alloc] init];
    
    //    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    self.backButton = backButton;
    
    [self.view addSubview:self.backButton];
    
    self.accountText = [self setupTextFiled:@"请输入手机号"];
    
    [self.view addSubview:self.accountText];
    
    self.passwordText = [self setupTextFiled:@"请输入验证码"];
    
    [self.view addSubview:self.passwordText];
    
    self.repasswordText = [self setupTextFiled:@"请输入新的密码"];
     self.repasswordText.secureTextEntry = YES;
    
    [self.view addSubview:self.repasswordText];
    
    
    UIButton * verifyButton = [[UIButton alloc] init];
    
    [verifyButton setTitle:@" 发送验证码 " forState:UIControlStateNormal];
    verifyButton.titleLabel.font = [UIFont boldSystemFontOfSize:13 * SCREENH6];
    [verifyButton setBackgroundColor:ColorWithRGB(0xDA4136)];
    
    [verifyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [verifyButton addTarget:self action:@selector(didVerifyButton) forControlEvents:UIControlEventTouchUpInside];
    //  把图片设置成圆形
    verifyButton.layer.cornerRadius = 15;//裁成圆角
    verifyButton.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    
    self.verifyButton = verifyButton;
    
    [self.view addSubview:self.verifyButton];
    
    
    UIButton * sureButton = [[UIButton alloc] init];
    
    [sureButton setTitle:@"登录" forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont boldSystemFontOfSize:17 * SCREENH6];
    [sureButton setBackgroundColor:ColorWithRGB(0xDA4136)];
    
    [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(didSureButton) forControlEvents:UIControlEventTouchUpInside];
    //  把图片设置成圆形
    sureButton.layer.cornerRadius = 25;//裁成圆角
    sureButton.layer.masksToBounds = YES;//隐藏裁剪掉的部分
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
    
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xFFFFFF);
    lineView.alpha = 0.4;
    
    self.lineView = lineView;
    
    [self.view addSubview:self.lineView];
    
}


-(void)viewDidLayoutSubviews{
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.backImageView).offset(25 * SCREENH6);
        make.width.mas_equalTo(12 * SCREENH6);
        make.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.accountText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(54 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-54 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(240 * SCREENH6);
    }];
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.height.mas_equalTo(self.accountText);
        make.width.mas_equalTo(170 * SCREENH6);
        make.top.mas_equalTo(self.accountText.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.verifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.passwordText);
        make.leading.mas_equalTo(self.passwordText.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.repasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.accountText);
        make.top.mas_equalTo(self.passwordText.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(80 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-80 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
        make.top.mas_equalTo(self.repasswordText.mas_bottom).offset(30 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.accountText);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.sureButton.mas_bottom).offset(60 * SCREENH6);
    }];
}


- (void)didVerifyButton{
    
    self.verifyButton.enabled = NO;
    secondsCountDown = 60;
    
    [self setupDownTimer];
}

- (void)didSureButton{
    
    [self.accountText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.repasswordText resignFirstResponder];
    
    if ([self.accountText.text isEqualToString:@""] || [self.passwordText.text isEqualToString:@""] || [self.repasswordText.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善注册信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        SHOWHUD;
        
        NSDictionary * parametersDic = @{@"mobile":self.accountText.text,@"password":self.repasswordText.text,@"code":self.passwordText.text};
        
        [[NetworkTool sharedTools] POST:@"/index/Member/forgetpwd" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            HIDDENHUD;
            
            NSLog(@"res===%@",responseObject);
            
            NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            
            if ([codeString isEqualToString:@"200"]) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你修改成功！赶快去登录吧！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            HIDDENHUD;
            
            NSLog(@"error===%@",error);
        }];
        
    }
}



- (void)setupDownTimer{
    
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
}

//定时器的方法
- (void)countDownAction{
    
    secondsCountDown --;
    
    [self.verifyButton setTitle:[NSString stringWithFormat:@"  %lds后获取  ",(long)secondsCountDown] forState:UIControlStateDisabled];
    
    [self.verifyButton setBackgroundColor:[UIColor lightGrayColor]];
    
    //当倒计时到0时做需要的操作，button变成可选状态
    if(secondsCountDown == 0){
        
        [_countDownTimer invalidate];
        
        [self.verifyButton setBackgroundColor:ColorWithRGB(0xDA4136)];
        [self.verifyButton setTitle:@"  获取验证码  " forState:UIControlStateNormal];
        self.verifyButton.enabled = YES;
    }
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.accountText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.repasswordText resignFirstResponder];
}


- (UITextField *)setupTextFiled:(NSString *)string{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = [UIColor blackColor];
    textFiled.alpha = 0.8;
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    //    passwordText.delegate = self;
    //设置光标颜色
    textFiled.tintColor = [UIColor whiteColor];
    //修改占位符文字颜色
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:string attributes:attrs];
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.textColor = [UIColor whiteColor];
    textFiled.textAlignment = NSTextAlignmentCenter;
    textFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    
    return textFiled;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
